package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.vo.OaProjectInitiationVoA;
import com.ruoyi.system.mapper.OaProjectNumberMapper;
import com.ruoyi.system.service.IOaIndustryAffiliationService;
import com.ruoyi.system.service.IOaSecondaryUnitService;
import com.ruoyi.system.service.IOaUnitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.OaProjectInitiationMapper;
import com.ruoyi.system.service.IOaProjectInitiationService;

/**
 * 记录项目立项的相关信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-10
 */
@Service
public class OaProjectInitiationServiceImpl implements IOaProjectInitiationService {
    @Autowired
    private OaProjectInitiationMapper oaProjectInitiationMapper;
    @Autowired
    private IOaIndustryAffiliationService oaIndustryAffiliationService;
    @Autowired
    private IOaUnitService oaUnitService;
    @Autowired
    private IOaSecondaryUnitService oaSecondaryUnitService;
    /**
     * 查询记录项目立项的相关信息
     *
     * @param id 记录项目立项的相关信息主键
     * @return 记录项目立项的相关信息
     */
    @Override
    public OaProjectInitiation selectOaProjectInitiationById(Integer id) {
        return oaProjectInitiationMapper.selectOaProjectInitiationById(id);
    }

    /**
     * 查询记录项目立项的相关信息列表
     *
     * @param oaProjectInitiation 记录项目立项的相关信息
     * @return 记录项目立项的相关信息
     */
    @Override
    public List<OaProjectInitiation> selectOaProjectInitiationList(OaProjectInitiation oaProjectInitiation) {
        return oaProjectInitiationMapper.selectOaProjectInitiationList(oaProjectInitiation);
    }

    @Override
    public List<OaProjectInitiation> selectOaProjectInitiationList1(OaProjectInitiation oaProjectInitiation) {
        return oaProjectInitiationMapper.selectOaProjectInitiationList1(oaProjectInitiation);
    }

    @Override
    public List<OaProjectInitiation> selectOaProjectInitiationList2(OaProjectInitiationVoA oaProjectInitiation) {
        return oaProjectInitiationMapper.selectOaProjectInitiationList2(oaProjectInitiation);
    }

    /**
     * 新增记录项目立项的相关信息
     *
     * @param oaProjectInitiation 记录项目立项的相关信息
     * @return 结果
     */
    @Override
    public int insertOaProjectInitiation(OaProjectInitiation oaProjectInitiation) {
        String company = oaProjectInitiation.getCompany();
        String industry1 = oaProjectInitiation.getIndustry();
        String secondaryCompany = oaProjectInitiation.getSecondaryCompany();
        OaIndustryAffiliation oaIndustryAffiliation = oaIndustryAffiliationService.selectOaIndustryAffiliationById(Long.valueOf(industry1));
        OaUnit oaUnit = oaUnitService.selectOaUnitById(Long.valueOf(company));
        OaSecondaryUnit oaSecondaryUnit = oaSecondaryUnitService.selectOaSecondaryUnitById(Long.valueOf(secondaryCompany));
        oaProjectInitiation.setCompany(oaUnit.getUnitName());
        oaProjectInitiation.setIndustry(oaIndustryAffiliation.getIndustry());
        oaProjectInitiation.setSecondaryCompany(oaSecondaryUnit.getSecondaryUnitName());

        LoginUser loginUser = SecurityUtils.getLoginUser();
        oaProjectInitiation.setInitiationPersonInCharge(loginUser.getUsername());
        oaProjectInitiation.setManagementApproval("0");
        oaProjectInitiation.setManagementApprovala("0");
        oaProjectInitiation.setFinancialApproval("0");
        oaProjectInitiation.setManagementApprovalas("0");
        oaProjectInitiation.setManagementApprovals("0");
        oaProjectInitiation.setReservedField2("1");
        oaProjectInitiation.setComplete(0);
        oaProjectInitiation.setCreationDate(new java.util.Date());
        oaProjectInitiation.setIsInvoiceOpened("否");
        oaProjectInitiation.setInvoiceStatus("否");
        oaProjectInitiation.setReceivablesStatus("否");
        BigDecimal totalAmount = BigDecimal.ZERO;
        oaProjectInitiation.setTotalPaymentAmount(totalAmount);
        oaProjectInitiation.setTotalActualAmount(totalAmount);
        oaProjectInitiation.setTotalBudgetAmount(totalAmount);
        // 生成项目编号
        String number = oaSecondaryUnit.getNumber();
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        // 定义日期时间格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMM");
        // 格式化日期，只保留年月
        String yearMonth = currentDate.format(formatter);

        String baseProjectNumber = number + yearMonth;
        int sequence = 1;
        String projectNumber;

        do {
            projectNumber = baseProjectNumber + sequence;
            OaProjectInitiation checkProject = new OaProjectInitiation();
            checkProject.setProjectNumber(projectNumber);
            List<OaProjectInitiation> existingProjects = oaProjectInitiationMapper.selectOaProjectInitiationList(checkProject);
            if (existingProjects.isEmpty()) {
                break;
            }
            sequence++;
        } while (true);

        oaProjectInitiation.setProjectNumber(projectNumber);

        return oaProjectInitiationMapper.insertOaProjectInitiation(oaProjectInitiation);
    }

    /**
     * 修改记录项目立项的相关信息
     *
     * @param oaProjectInitiation 记录项目立项的相关信息
     * @return 结果
     */
    @Override
    public int updateOaProjectInitiation(OaProjectInitiation oaProjectInitiation) {
        return oaProjectInitiationMapper.updateOaProjectInitiation(oaProjectInitiation);
    }

    /**
     * 批量删除记录项目立项的相关信息
     *
     * @param ids 需要删除的记录项目立项的相关信息主键
     * @return 结果
     */
    @Override
    public int deleteOaProjectInitiationByIds(Integer[] ids) {
        return oaProjectInitiationMapper.deleteOaProjectInitiationByIds(ids);
    }

    /**
     * 删除记录项目立项的相关信息信息
     *
     * @param id 记录项目立项的相关信息主键
     * @return 结果
     */
    @Override
    public int deleteOaProjectInitiationById(Integer id) {
        return oaProjectInitiationMapper.deleteOaProjectInitiationById(id);
    }

    @Override
    public List<OaProjectInitiation> selectOaProjectInitiationList3(OaProjectInitiation oaProjectInitiation1) {
        return oaProjectInitiationMapper.selectOaProjectInitiationList3(oaProjectInitiation1);
    }

    @Override
    public List<OaProjectInitiation> selectOaProjectInitiationList4(OaProjectInitiation oaProjectInitiation1) {
        return oaProjectInitiationMapper.selectOaProjectInitiationList4(oaProjectInitiation1);
    }
}
